<!DOCTYPE html>
<html lang="en">
  <head>
    <title>
        Docker-2-网络 - rulerLwx Blog
      </title>
        <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-transform" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no,email=no,adress=no">
    
    <meta name="theme-color" content="#000000" />
    
    <meta http-equiv="window-target" content="_top" />
    
    
    <meta name="description" content="容器网络 如何查看容器的IP 命令：docker inspect 容器名 .... &amp;quot;Networks&amp;quot;: { &amp;quot;bridge&amp;quot;: { &amp;quot;IPAMConfig&amp;quot;: null, &amp;quot;Links&amp;quot;: null, &amp;quot;Aliases&amp;quot;: null, &amp;quot;NetworkID&amp;quot;: &amp;quot;c167bb0d8d95a088e76ccfff410704298be4f666f7a288a14c2498bc145a1edb&amp;quot;, &amp;quot;EndpointID&amp;quot;: &amp;quot;83687cf0e4565af8c9eda1427b4c208bc09a2c9f9ae498d53ca849933ce54905&amp;quot;, &amp;quot;Gateway&amp;quot;: &amp;quot;172.17.0.1&amp;quot;, &amp;quot;IPAddress&amp;quot;: &amp;quot;172.17.0.3&amp;quot;, &amp;quot;IPPrefixLen&amp;quot;: 16, &amp;quot;IPv6Gateway&amp;quot;: &amp;quot;&amp;quot;, &amp;quot;GlobalIPv6Address&amp;quot;: &amp;quot;&amp;quot;, &amp;quot;GlobalIPv6PrefixLen&amp;quot;: 0, &amp;quot;MacAddress&amp;quot;: &amp;quot;02:42:ac:11:00:03&amp;quot; } } .... Docker原生网络 学习" />
    <meta name="generator" content="Hugo 0.73.0 with theme pure" />
    <title>Docker-2-网络 - rulerLwx Blog</title>
    
    
    <link rel="stylesheet" href="https://rulerLwx.gitee.io/css/style.min.c4bc7071f132c964c2116bca53b392933f377e5ca7b7051ed245187c621a2d3e.css">
    
    <link rel="stylesheet" href="https://cdn.staticfile.org/highlight.js/9.15.10/styles/github.min.css" async>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.4.2/tocbot.css" async>
    <meta property="og:title" content="Docker-2-网络" />
<meta property="og:description" content="容器网络 如何查看容器的IP 命令：docker inspect 容器名 .... &quot;Networks&quot;: { &quot;bridge&quot;: { &quot;IPAMConfig&quot;: null, &quot;Links&quot;: null, &quot;Aliases&quot;: null, &quot;NetworkID&quot;: &quot;c167bb0d8d95a088e76ccfff410704298be4f666f7a288a14c2498bc145a1edb&quot;, &quot;EndpointID&quot;: &quot;83687cf0e4565af8c9eda1427b4c208bc09a2c9f9ae498d53ca849933ce54905&quot;, &quot;Gateway&quot;: &quot;172.17.0.1&quot;, &quot;IPAddress&quot;: &quot;172.17.0.3&quot;, &quot;IPPrefixLen&quot;: 16, &quot;IPv6Gateway&quot;: &quot;&quot;, &quot;GlobalIPv6Address&quot;: &quot;&quot;, &quot;GlobalIPv6PrefixLen&quot;: 0, &quot;MacAddress&quot;: &quot;02:42:ac:11:00:03&quot; } } .... Docker原生网络 学习" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://rulerLwx.gitee.io/2017/08/docker-2-%E7%BD%91%E7%BB%9C/" />
<meta property="article:published_time" content="2017-08-01T10:03:01+08:00" />
<meta property="article:modified_time" content="2017-08-01T10:03:01+08:00" />
<meta itemprop="name" content="Docker-2-网络">
<meta itemprop="description" content="容器网络 如何查看容器的IP 命令：docker inspect 容器名 .... &quot;Networks&quot;: { &quot;bridge&quot;: { &quot;IPAMConfig&quot;: null, &quot;Links&quot;: null, &quot;Aliases&quot;: null, &quot;NetworkID&quot;: &quot;c167bb0d8d95a088e76ccfff410704298be4f666f7a288a14c2498bc145a1edb&quot;, &quot;EndpointID&quot;: &quot;83687cf0e4565af8c9eda1427b4c208bc09a2c9f9ae498d53ca849933ce54905&quot;, &quot;Gateway&quot;: &quot;172.17.0.1&quot;, &quot;IPAddress&quot;: &quot;172.17.0.3&quot;, &quot;IPPrefixLen&quot;: 16, &quot;IPv6Gateway&quot;: &quot;&quot;, &quot;GlobalIPv6Address&quot;: &quot;&quot;, &quot;GlobalIPv6PrefixLen&quot;: 0, &quot;MacAddress&quot;: &quot;02:42:ac:11:00:03&quot; } } .... Docker原生网络 学习">
<meta itemprop="datePublished" content="2017-08-01T10:03:01&#43;08:00" />
<meta itemprop="dateModified" content="2017-08-01T10:03:01&#43;08:00" />
<meta itemprop="wordCount" content="1770">



<meta itemprop="keywords" content="," /><meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="Docker-2-网络"/>
<meta name="twitter:description" content="容器网络 如何查看容器的IP 命令：docker inspect 容器名 .... &quot;Networks&quot;: { &quot;bridge&quot;: { &quot;IPAMConfig&quot;: null, &quot;Links&quot;: null, &quot;Aliases&quot;: null, &quot;NetworkID&quot;: &quot;c167bb0d8d95a088e76ccfff410704298be4f666f7a288a14c2498bc145a1edb&quot;, &quot;EndpointID&quot;: &quot;83687cf0e4565af8c9eda1427b4c208bc09a2c9f9ae498d53ca849933ce54905&quot;, &quot;Gateway&quot;: &quot;172.17.0.1&quot;, &quot;IPAddress&quot;: &quot;172.17.0.3&quot;, &quot;IPPrefixLen&quot;: 16, &quot;IPv6Gateway&quot;: &quot;&quot;, &quot;GlobalIPv6Address&quot;: &quot;&quot;, &quot;GlobalIPv6PrefixLen&quot;: 0, &quot;MacAddress&quot;: &quot;02:42:ac:11:00:03&quot; } } .... Docker原生网络 学习"/>

    <!--[if lte IE 9]>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/classlist/1.1.20170427/classList.min.js"></script>
      <![endif]-->

    <!--[if lt IE 9]>
        <script src="https://cdn.jsdelivr.net/npm/html5shiv@3.7.3/dist/html5shiv.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/respond.js@1.4.2/dest/respond.min.js"></script>
      <![endif]-->
  </head>

  
  

  <body class="main-center theme-black" itemscope itemtype="http://schema.org/WebPage"><header class="header" itemscope itemtype="http://schema.org/WPHeader">
    <div class="slimContent">
      <div class="navbar-header">
        <div class="profile-block text-center">
          <a id="avatar" href="https://gitee.com/rulerLwx" target="_blank">
            <img class="img-circle img-rotate" src="https://rulerLwx.gitee.io/avatar.png" width="200" height="200">
          </a>
          <h2 id="name" class="hidden-xs hidden-sm">rulerLwx</h2>
          <h3 id="title" class="hidden-xs hidden-sm hidden-md">thinking...</h3>
          <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i>Guangzhou, China</small>
        </div><div class="search" id="search-form-wrap">
    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="Search" />
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i
                        class="icon icon-search"></i></button>
            </span>
        </div>
        <div class="ins-search">
            <div class="ins-search-mask"></div>
            <div class="ins-search-container">
                <div class="ins-input-wrapper">
                    <input type="text" class="ins-search-input" placeholder="Type something..."
                        x-webkit-speech />
                    <button type="button" class="close ins-close ins-selectable" data-dismiss="modal"
                        aria-label="Close"><span aria-hidden="true">×</span></button>
                </div>
                <div class="ins-section-wrapper">
                    <div class="ins-section-container"></div>
                </div>
            </div>
        </div>
    </form>
</div>
        <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
      </div>
      <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
        <ul class="nav navbar-nav main-nav">
            <li class="menu-item menu-item-home">
                <a href="/">
                    <i class="icon icon-home-fill"></i>
                  <span class="menu-title">Home</span>
                </a>
            </li>
            <li class="menu-item menu-item-archives">
                <a href="/posts/">
                    <i class="icon icon-archives-fill"></i>
                  <span class="menu-title">Archives</span>
                </a>
            </li>
            <li class="menu-item menu-item-categories">
                <a href="/categories/">
                    <i class="icon icon-folder"></i>
                  <span class="menu-title">Categories</span>
                </a>
            </li>
            <li class="menu-item menu-item-tags">
                <a href="/tags/">
                    <i class="icon icon-tags"></i>
                  <span class="menu-title">Tags</span>
                </a>
            </li>
            <li class="menu-item menu-item-about">
                <a href="/about/">
                    <i class="icon icon-cup-fill"></i>
                  <span class="menu-title">About</span>
                </a>
            </li>
        </ul>
      </nav>
    </div>
  </header>

<aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">Board</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">enjoy~
            </div>
        </div>
    </div>
</div>

      <div class="widget">
    <h3 class="widget-title"> Categories</h3>
    <div class="widget-body">
        <ul class="category-list">
            <li class="category-list-item"><a href="https://rulerLwx.gitee.io/categories/java-framework/" class="category-list-link">java-framework</a><span class="category-list-count">38</span></li>
            <li class="category-list-item"><a href="https://rulerLwx.gitee.io/categories/java-front-end/" class="category-list-link">java-front-end</a><span class="category-list-count">11</span></li>
            <li class="category-list-item"><a href="https://rulerLwx.gitee.io/categories/java-se/" class="category-list-link">java-se</a><span class="category-list-count">21</span></li>
            <li class="category-list-item"><a href="https://rulerLwx.gitee.io/categories/java-senior/" class="category-list-link">java-senior</a><span class="category-list-count">4</span></li>
            <li class="category-list-item"><a href="https://rulerLwx.gitee.io/categories/linux/" class="category-list-link">linux</a><span class="category-list-count">13</span></li>
            <li class="category-list-item"><a href="https://rulerLwx.gitee.io/categories/tools/" class="category-list-link">tools</a><span class="category-list-count">1</span></li>
            <li class="category-list-item"><a href="https://rulerLwx.gitee.io/categories/%E6%8A%80%E6%9C%AF%E6%9D%82%E7%83%A9/" class="category-list-link">技术杂烩</a><span class="category-list-count">4</span></li>
            <li class="category-list-item"><a href="https://rulerLwx.gitee.io/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/" class="category-list-link">数据库</a><span class="category-list-count">15</span></li>
        </ul>
    </div>
</div>
      <div class="widget">
    <h3 class="widget-title"> Tags</h3>
    <div class="widget-body">
        <ul class="tag-list">
            
            
            <li class="tag-list-item"><a href="https://rulerLwx.gitee.io/tags/jvm/" class="tag-list-link">jvm</a><span
                    class="tag-list-count">1</span></li>
            
            
            <li class="tag-list-item"><a href="https://rulerLwx.gitee.io/tags/%E5%A4%9A%E7%BA%BF%E7%A8%8B/" class="tag-list-link">多线程</a><span
                    class="tag-list-count">2</span></li>
            
            
            <li class="tag-list-item"><a href="https://rulerLwx.gitee.io/tags/%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/" class="tag-list-link">网络编程</a><span
                    class="tag-list-count">3</span></li>
            
        </ul>

    </div>
</div>
      
<div class="widget">
    <h3 class="widget-title">Recent Posts</h3>
    <div class="widget-body">
        <ul class="recent-post-list list-unstyled no-thumbnail">
            <li>
                <div class="item-inner">
                    <p class="item-title">
                        <a href="https://rulerLwx.gitee.io/2020/07/%E8%87%AA%E5%B7%B1%E5%8A%A8%E6%89%8B%E4%B8%80%E6%89%B9%E9%87%8F%E7%A7%BB%E5%8A%A8%E9%87%8D%E5%91%BD%E5%90%8D%E6%96%87%E4%BB%B6/" class="title">自己动手（一）——批量移动、重命名文件</a>
                    </p>
                    <p class="item-date">
                        <time datetime="2020-07-27 17:50:02 &#43;0800 CST" itemprop="datePublished">2020-07-27</time>
                    </p>
                </div>
            </li>
            <li>
                <div class="item-inner">
                    <p class="item-title">
                        <a href="https://rulerLwx.gitee.io/2020/07/%E5%85%B3%E4%BA%8Einteger%E7%9A%84-128~127%E7%BC%93%E5%AD%98/" class="title">关于Integer的 -128~127缓存</a>
                    </p>
                    <p class="item-date">
                        <time datetime="2020-07-11 16:56:21 &#43;0800 CST" itemprop="datePublished">2020-07-11</time>
                    </p>
                </div>
            </li>
            <li>
                <div class="item-inner">
                    <p class="item-title">
                        <a href="https://rulerLwx.gitee.io/2020/07/%E8%B7%A8%E5%9F%9F%E9%97%AE%E9%A2%98/" class="title">跨域问题</a>
                    </p>
                    <p class="item-date">
                        <time datetime="2020-07-08 22:41:12 &#43;0800 CST" itemprop="datePublished">2020-07-08</time>
                    </p>
                </div>
            </li>
            <li>
                <div class="item-inner">
                    <p class="item-title">
                        <a href="https://rulerLwx.gitee.io/2020/07/%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2%E6%90%AD%E5%BB%BA/" class="title">个人博客搭建</a>
                    </p>
                    <p class="item-date">
                        <time datetime="2020-07-05 18:58:47 &#43;0800 CST" itemprop="datePublished">2020-07-05</time>
                    </p>
                </div>
            </li>
            <li>
                <div class="item-inner">
                    <p class="item-title">
                        <a href="https://rulerLwx.gitee.io/2020/07/mongodb-1/" class="title">MonggoDB-1</a>
                    </p>
                    <p class="item-date">
                        <time datetime="2020-07-03 18:58:47 &#43;0800 CST" itemprop="datePublished">2020-07-03</time>
                    </p>
                </div>
            </li>
        </ul>
    </div>
</div>
  </div>
</aside>

    
    
<aside class="sidebar sidebar-toc collapse" id="collapseToc" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    <h4 class="toc-title">Catalogue</h4>
    <nav id="toc" class="js-toc toc">

    </nav>
  </div>
</aside>
<main class="main" role="main"><div class="content">
  <article id="-" class="article article-type-" itemscope
    itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      <h1 itemprop="name">
  <a
    class="article-title"
    href="/2017/08/docker-2-%E7%BD%91%E7%BB%9C/"
    >Docker-2-网络</a
  >
</h1>

      <div class="article-meta">
        
<span class="article-date">
  <i class="icon icon-calendar-check"></i>&nbsp;
<a href="https://rulerLwx.gitee.io/2017/08/docker-2-%E7%BD%91%E7%BB%9C/" class="article-date">
  <time datetime="2017-08-01 10:03:01 &#43;0800 CST" itemprop="datePublished">2017-08-01</time>
</a>
</span>
<span class="article-category">
  <i class="icon icon-folder"></i>&nbsp;
  <a class="article-category-link" href="/categories/linux/"> Linux </a>
</span>  
  <span class="article-tag">
    <i class="icon icon-tags"></i>&nbsp;
    <a class="article-tag-link" href="/tags//">  </a>
  </span>

        <span class="post-comment"><i class="icon icon-comment"></i>&nbsp;<a href="/2017/08/docker-2-%E7%BD%91%E7%BB%9C/#comments"
            class="article-comment-link">Comments</a></span>
		<span class="post-wordcount hidden-xs" itemprop="wordCount">Word Count: 1770words</span>
		<span class="post-readcount hidden-xs" itemprop="timeRequired">Read Count: 4minutes </span>
      </div>
    </div>
    <div class="article-entry marked-body js-toc-content" itemprop="articleBody">
      <h1 id="容器网络">容器网络</h1>
<h2 id="如何查看容器的ip">如何查看容器的IP</h2>
<p>命令：<code>docker inspect 容器名</code></p>
<pre><code>    ....

    &quot;Networks&quot;: {
        &quot;bridge&quot;: {
            &quot;IPAMConfig&quot;: null,
            &quot;Links&quot;: null,
            &quot;Aliases&quot;: null,
            &quot;NetworkID&quot;: &quot;c167bb0d8d95a088e76ccfff410704298be4f666f7a288a14c2498bc145a1edb&quot;,
            &quot;EndpointID&quot;: &quot;83687cf0e4565af8c9eda1427b4c208bc09a2c9f9ae498d53ca849933ce54905&quot;,
            &quot;Gateway&quot;: &quot;172.17.0.1&quot;,
            &quot;IPAddress&quot;: &quot;172.17.0.3&quot;,
            &quot;IPPrefixLen&quot;: 16,
            &quot;IPv6Gateway&quot;: &quot;&quot;,
            &quot;GlobalIPv6Address&quot;: &quot;&quot;,
            &quot;GlobalIPv6PrefixLen&quot;: 0,
            &quot;MacAddress&quot;: &quot;02:42:ac:11:00:03&quot;
        }
    }
    ....

</code></pre>
<h2 id="docker原生网络">Docker原生网络</h2>
<p>学习资料：https://www.bilibili.com/video/av55976953/?p=13</p>
<p>docker安装后默认自动创建3种网络：bridge/host/none</p>
<pre><code class="language-shell">[root@localhost ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
58214525774f        bridge              bridge              local
dd0e78f25340        host                host                local
1534922da50b        none                null                local
</code></pre>
<p>docker 安装时会创建一个名为 docker0 的 Linux bridge，新建的容器会自动桥接到这个接口</p>
<pre><code class="language-shell">[root@localhost ~]# ip addr show docker0
3: docker0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:f8:e0:bb:ac brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.1/16 scope global docker0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:f8ff:fee0:bbac/64 scope link 
       valid_lft forever preferred_lft forever

</code></pre>
<p>新建的容器的IP会依次自动递增，如 172.17.0.2/16，172.17.0.3/16</p>
<p>1）bridge模式（默认）</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img-07/20200708162911.png" alt=""></p>
<p>2）host模式</p>
<p>要使用 host 网络模式，则需要在容器创建时指定：&ndash;network=host</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img-07/20200708162947.png" alt=""></p>
<p>此时在容器中使用 ifconfig 得到的网络跟宿主机的一样</p>
<p>3）none模式</p>
<p>关键点：禁用网络</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img-07/20200708163008.png" alt=""></p>
<h2 id="自定义网络">自定义网络</h2>
<p>docker 提供了3种自定义网络驱动：</p>
<ul>
<li>bridge</li>
<li>overlay</li>
<li>macvlan</li>
</ul>
<p>此处的 bridge，类似于默认的bridge网络模式，但增加了一些新的功能；overlay、macvlan用于创建主机网络</p>
<p>建议使用自定义的网络来控制哪些容器可以相互通信，还可以DNS解析容器名称到IP地址</p>
<p>如何创建自定义网络？</p>
<p>1）创建自定义网桥</p>
<pre><code class="language-shell">[root@localhost ~]# docker network create -d bridge my_net1
e3e5cd5694b2cc3b0d0345a7564b9d200300d5a8196301973d53fd38814e6a24
</code></pre>
<p>-d bridge 表示指定使用的驱动，默认是 bridge，因此可以不写</p>
<p>还可以自定义网段：在创建时指定参数 &ndash;subnet、&ndash;geteway</p>
<pre><code class="language-shell">[root@localhost ~]# docker network create -d bridge --subnet 172.22.0.0/24 --gateway 172.22.0.1  my_net2
2795741963a53d10206568ca7688b1ed2d7df1873f877014c7f160b8f9791e4f
[root@localhost ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
58214525774f        bridge              bridge              local
dd0e78f25340        host                host                local
e3e5cd5694b2        my_net1             bridge              local
2795741963a5        my_net2             bridge              local
1534922da50b        none                null                local
</code></pre>
<p>2）创建一个容器，并使用上面的 my_net1 网桥</p>
<pre><code class="language-shell">[root@localhost ~]# docker run --name vm1 -it --network my_net1 centos
</code></pre>
<p>查看网桥详细信息</p>
<pre><code class="language-shell">[root@localhost ~]# docker network inspect my_net1
[
    {
        &quot;Name&quot;: &quot;my_net1&quot;,
        &quot;Id&quot;: &quot;e3e5cd5694b2cc3b0d0345a7564b9d200300d5a8196301973d53fd38814e6a24&quot;,
        &quot;Created&quot;: &quot;2019-07-12T19:27:21.239734462+08:00&quot;,
        &quot;Scope&quot;: &quot;local&quot;,
        &quot;Driver&quot;: &quot;bridge&quot;,
        &quot;EnableIPv6&quot;: false,
        &quot;IPAM&quot;: {
            &quot;Driver&quot;: &quot;default&quot;,
            &quot;Options&quot;: {},
            &quot;Config&quot;: [
                {
                    &quot;Subnet&quot;: &quot;172.18.0.0/16&quot;,
                    &quot;Gateway&quot;: &quot;172.18.0.1&quot;
                }
            ]
        },
        &quot;Internal&quot;: false,
        &quot;Attachable&quot;: false,
        &quot;Containers&quot;: {
            &quot;179545cecca5294bbd5f778ebb533d12179169eef0b041054d742122597e4018&quot;: {
                &quot;Name&quot;: &quot;vm1&quot;,
                &quot;EndpointID&quot;: &quot;e6e6b308a2720d88a287709c2801a4ff45d08ddfc0245a963b7bae1a0f2c50ff&quot;,
                &quot;MacAddress&quot;: &quot;02:42:ac:12:00:02&quot;,
                &quot;IPv4Address&quot;: &quot;172.18.0.2/16&quot;,
                &quot;IPv6Address&quot;: &quot;&quot;
            }
        },
        &quot;Options&quot;: {},
        &quot;Labels&quot;: {}
    }
]
</code></pre>
<p>另外，对于自定义网段、网关的 my_net2，创建容器时还可使用<code>--ip</code>参数指定IP（my_net1不支持，因为不是自定义网段、网关）</p>
<pre><code class="language-shell">[root@localhost ~]# docker run --name vm2 -it --network my_net2 --ip 172.22.0.10 centos
</code></pre>
<p>在同一网桥上（如my_net2 ）容器是互通的</p>
<p>桥接到不同网桥上容器，彼此是不能通信的；docker在设计上就是要隔离不同的network。如 172.22.0.10 是ping不通 172.18.0.2。</p>
<p>如何让不同网桥上的容器通信呢：使用<code>docker network connect </code>命令为vm1添加一块my_net2的网卡</p>
<pre><code class="language-shell">[root@localhost ~]# docker network connect my_net2 vm1
</code></pre>
<p>这样vm1就能ping通vm2，但此时vm2是拼不能vm1的 ——20190713实践过，需要同样添加网卡才行：</p>
<pre><code class="language-shell">[root@localhost ~]# docker network connect my_net1 vm2
</code></pre>
<h2 id="容器间通信">容器间通信</h2>
<p>1）容器之间通信除了使用IP通信外，还可以使用容器名称通信</p>
<ul>
<li>docker从1.10开始，内嵌了一个DNS server</li>
<li><strong>dns解析功能必须在自定义网络中使用</strong>，docker原生的3种网络不能使用</li>
<li>启动容器时使用 &ndash;name 参数指定容器名称</li>
</ul>
<p>在vm2容器中拼vm1：</p>
<pre><code class="language-shell">[root@7ba00cd3fe5a /]# ping vm1
PING vm1 (172.18.0.2) 56(84) bytes of data.
64 bytes from vm1.my_net1 (172.18.0.2): icmp_seq=1 ttl=64 time=0.053 ms
64 bytes from vm1.my_net1 (172.18.0.2): icmp_seq=2 ttl=64 time=0.168 ms
</code></pre>
<p>2）Joined</p>
<p>两台容器共享一个网络栈（类似于主机模式），两个容器可以使用Localhost快速高效地通信</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img-07/20200708163100.png" alt=""></p>
<p>3）&ndash;link</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img-07/20200708163112.png" alt=""></p>
<p>4）容器是如何访问外网的</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img-07/20200708163123.png" alt=""></p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img-07/20200708163223.png" alt=""></p>
<h2 id="跨主机容器通信macvlan方式">跨主机容器通信——macvlan方式</h2>
<p>跨主机网络解决方案：</p>
<ul>
<li>docker原生的 overlay、macvlan</li>
<li>第三方的 flannel、weave、calico</li>
</ul>
<p>众多网络方案是如何与docker集成在一起的：</p>
<ul>
<li>libnetwork，docker容器网络库</li>
<li>CNM(Container Network Model)这个模型对容器网络进行了抽象</li>
</ul>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img-07/20200708163257.png" alt=""></p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img-07/20200708163307.png" alt=""></p>
<p>使用 macvlan实现两台主机之间的容器通信：</p>
<p>1）</p>
<p>先清理掉多余的网桥：</p>
<pre><code class="language-shell">[root@localhost ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
e3ca980cb0de        bridge              bridge              local
dd0e78f25340        host                host                local
e3e5cd5694b2        my_net1             bridge              local
2795741963a5        my_net2             bridge              local
1534922da50b        none                null                local
[root@localhost ~]# docker network prune 
WARNING! This will remove all networks not used by at least one container.
Are you sure you want to continue? [y/N] y
Deleted Networks:
my_net1
my_net2

[root@localhost ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
e3ca980cb0de        bridge              bridge              local
dd0e78f25340        host                host                local
1534922da50b        none                null                local
</code></pre>
<p>准备两台主机，并各自添加一个新的网卡</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img-07/20200708163331.png" alt=""></p>
<p>2）打开混杂模式</p>
<p>为两台主机新添加的网卡打开混杂模式</p>
<pre><code class="language-shell">
</code></pre>
<p>3）</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img-07/20200708163401.png" alt=""></p>
<p>4）</p>
<p><img src="https://gitee.com/rulerLwx/PicGo/raw/master/img-07/20200708163416.png" alt=""></p>
<h2 id="网络命令-汇总">网络命令-汇总</h2>
<pre><code class="language-shell"># docker network ls
# docker network rm # 删除网桥
# docker network inspect x # 查看网桥信息
# docker network prune # 删除不是docker默认创建的网桥
# docker network create -d bridge my_net1
# docker network connect my_net2 vm1
# docker network disconnect my_net2 vm1

# ip addr show docker0
# brctl show

</code></pre>
<h2 id="docker0">docker0</h2>
<pre><code class="language-shell">[root@localhost network-scripts]# ifconfig
docker0: flags=4099&lt;UP,BROADCAST,MULTICAST&gt;  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 0.0.0.0
        inet6 fe80::42:3aff:feb3:e49  prefixlen 64  scopeid 0x20&lt;link&gt;
        ether 02:42:3a:b3:0e:49  txqueuelen 0  (Ethernet)
        RX packets 32  bytes 2176 (2.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 8  bytes 656 (656.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
...
</code></pre>
<p>1）安装网桥工具</p>
<pre><code class="language-shell">yum install bridge-utils
</code></pre>
<p>2）查看本机上的网桥</p>
<pre><code class="language-shell">[root@localhost ~]# brctl show
bridge name	bridge id		STP enabled	interfaces
docker0		8000.02423ab30e49	no	
</code></pre>
<p>修改docker0地址</p>
<pre><code># ifconfig docker0 192.168.12.150 netmask 255.255.255.0
</code></pre>

    </div>
    <div class="article-footer">
<blockquote class="mt-2x">
  <ul class="post-copyright list-unstyled">
    <li class="post-copyright-link hidden-xs">
      <strong>Permalink: </strong>
      <a href="https://rulerLwx.gitee.io/2017/08/docker-2-%E7%BD%91%E7%BB%9C/" title="Docker-2-网络" target="_blank" rel="external">https://rulerLwx.gitee.io/2017/08/docker-2-%E7%BD%91%E7%BB%9C/</a>
    </li>
    <li class="post-copyright-license">
      <strong>License：</strong><a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN</a>
    </li>
  </ul>
</blockquote>

<div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="https://gitee.com/rulerLwx" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="https://rulerLwx.gitee.io/avatar.png" class="img-rounded w-full" alt="">
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="https://gitee.com/rulerLwx" target="_blank"><span class="text-dark">rulerLwx</span><small class="ml-1x">thinking...</small></a></h3>
        <div>Good Good Study, Day Day Up~</div>
      </div>
    </figure>
  </div>
</div>
    </div>
  </article>
<section id="comments">
    <div id="vcomments"></div>
</section>

</div><nav class="bar bar-footer clearfix" data-stick-bottom>
    <div class="bar-inner">
        <ul class="pager pull-left">
            <li class="prev">
                <a href="https://rulerLwx.gitee.io/2017/08/nginx-5-%E6%AD%A3%E5%8F%8D%E4%BB%A3%E7%90%86/" title="Nginx-5-正反代理"><i
                        class="icon icon-angle-left"
                        aria-hidden="true"></i><span>&nbsp;&nbsp;Older</span></a>
            </li>
            <li class="next">
                <a href="https://rulerLwx.gitee.io/2017/08/docker-3-%E5%B8%B8%E7%94%A8%E8%BD%AF%E4%BB%B6%E5%AE%89%E8%A3%85/"
                    title="Docker-3-常用软件安装"><span>Newer&nbsp;&nbsp;</span><i
                        class="icon icon-angle-right" aria-hidden="true"></i></a>
            </li>
            
            <li class="toggle-toc">
                <a class="toggle-btn collapsed" data-toggle="collapse" href="#collapseToc" aria-expanded="false"
                    title="Catalogue" role="button">
                    <span>[&nbsp;</span><span>Catalogue</span>
                    <i class="text-collapsed icon icon-anchor"></i>
                    <i class="text-in icon icon-close"></i>
                    <span>]</span>
                </a>
            </li>
        </ul>
        <div class="bar-right">
            <div class="share-component" data-sites="weibo,qq,wechat"
                data-mobile-sites="weibo,qq,qzone"></div>
        </div>
    </div>
</nav>

</main><footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
<ul class="social-links">
    <li><a href="https://gitee.com/rulerLwx" target="_blank" title="gitee" data-toggle=tooltip data-placement=top >
            <i class="icon icon-gitee"></i></a></li>
    <li><a href="https://github.com/wolf-lea" target="_blank" title="github" data-toggle=tooltip data-placement=top >
            <i class="icon icon-github"></i></a></li>
</ul>
  <div class="copyright">
    &copy;2017  -
    2020
    <div class="publishby">
        Theme by <a href="https://github.com/xiaoheiAh" target="_blank"> xiaoheiAh </a>base on<a href="https://github.com/xiaoheiAh/hugo-theme-pure" target="_blank"> pure</a>.
    </div>
  </div>
</footer>

<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-MML-AM_SVG"></script>
<script type="text/x-mathjax-config">
    MathJax.Hub.Config({
            showMathMenu: false, //disables context menu
            tex2jax: {
            inlineMath: [ ['$','$'], ['\\(','\\)'] ]
           }
    });
</script>


<script src="https://cdn.jsdelivr.net/npm/jquery@3.4.1/dist/jquery.min.js"></script>
<script>
    window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>
<script type="text/javascript" src="https://cdn.staticfile.org/highlight.js/9.15.10/highlight.min.js"></script>
<script type="text/javascript" src="https://cdn.staticfile.org/highlight.js/9.15.10/languages/python.min.js" defer></script>
<script type="text/javascript" src="https://cdn.staticfile.org/highlight.js/9.15.10/languages/javascript.min.js" defer></script><script>
    hljs.configure({
        tabReplace: '    ', 
        classPrefix: ''     
        
    })
    hljs.initHighlightingOnLoad();
</script>
<script src="https://rulerLwx.gitee.io/js/application.min.bdeb64b910570b6c41badc6a05b7afb0c8ad9efd8525de3c7257d59e786326a3.js"></script>
<script src="https://rulerLwx.gitee.io/js/plugin.min.51ff8c7317566f82259170fa36e09c4493adc9b9378b427a01ad3f017ebac7dd.js"></script>

<script>
    (function (window) {
        var INSIGHT_CONFIG = {
            TRANSLATION: {
                POSTS: 'Posts',
                PAGES: 'Pages',
                CATEGORIES: 'Categories',
                TAGS: 'Tags',
                UNTITLED: '(Untitled)',
            },
            ROOT_URL: 'https:\/\/rulerLwx.gitee.io',
            CONTENT_URL: 'https:\/\/rulerLwx.gitee.io\/searchindex.json ',
        };
        window.INSIGHT_CONFIG = INSIGHT_CONFIG;
    })(window);
</script>
<script type="text/javascript" src="https://rulerLwx.gitee.io/js/insight.min.a343cd9a5a7698336b28ef3a7c16a3a1b1d2d5fb17dc8ed04022bbe08cc5459073a15bdafa3a8a58cdd56080784bdd69fa70b1ae8597565c799c57ed00f0e120.js" defer></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tocbot/4.4.2/tocbot.min.js"></script>
<script>
    tocbot.init({
        
        tocSelector: '.js-toc',
        
        contentSelector: '.js-toc-content',
        
        headingSelector: 'h1, h2, h3',
        
        hasInnerContainers: true,
    });
</script>

<script src="https://cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/valine"></script>
<script type="text/javascript">
    var GUEST = ['nick', 'mail', 'link'];
    var meta = 'nick,mail';
    meta = meta.split(',').filter(function (item) {
        return GUEST.indexOf(item) > -1;
    });
    new Valine({
        el: '#vcomments',
        verify: null ,
        notify: null ,
        appId: 'IyAB0PSPRazTPDxitO1ddQ7O-gzGzoHsz',
        appKey: '5rBJTq4KidYF33eXwvRVhtEH',
        placeholder: 'enjoy~',
        avatar: 'mm',
        meta: meta,
        pageSize: '10' || 10,
        visitor: false 
});
</script>

  </body>
</html>
